﻿namespace BloodlustFury.DataMiner
{
	#region "Using Statements"

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Windows.Forms;

	#endregion

	public partial class NearestNeighborDialogBox : Form
	{
		public Int32 NearestNeighbors = 0;

		public NearestNeighborDialogBox()
		{
			InitializeComponent();
		}

		private void OkButton_Click(object sender, EventArgs e)
		{
			Int32 dummy;

			if (Int32.TryParse(NearestNeighborsTextBox.Text, out dummy))
			{
				this.NearestNeighbors = dummy;
				this.DialogResult = DialogResult.OK;
			}
			else
			{
				this.DialogResult = DialogResult.Cancel;
			}

			this.Close();
		}

		private void CancelButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;

			this.Close();
		}

		private void NearestNeighborsTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			// Try converting the introduced char value to a byte value
			byte i;
			bool parsed = byte.TryParse(e.KeyChar.ToString(), out i);
			bool val = true;

			//If convresion succeeded, show allow character.
			if (parsed)
			{
				val = false;
			}
			// If the conversion did not succeed, check if the pressed
			// button was Backspace (char code is 8) or Delete (char code is 46).
			else if (parsed == false)
			{
				switch (Convert.ToInt32(e.KeyChar))
				{
					default:
						val = true;
						break;
					case 8:
						val = false;  // If Backspace, allow char.
						break;
					case 46:
						val = false;  // If Delete, allow char.
						break;
				}
			}

			e.Handled = val;
		}
	}
}
